package com.example.websocket.controller;

import com.example.websocket.dao.ChatRoomMapper;
import com.example.websocket.dao.ChatRoomUserMapper;
import com.example.websocket.model.table.ChatRoom;
import com.example.websocket.model.table.User;
import com.example.websocket.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by liulanhua on 2018/9/25.
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private ChatRoomMapper roomMapper;
    @Autowired
    private ChatRoomUserMapper roomUserMapper;


    @GetMapping("/page/{id}")
    public ModelAndView userPage(@PathVariable("id") String id, ModelAndView mv) {
        User findUser = userService.findUser(new User(id));
        List<ChatRoom> roomList = roomMapper.selectList(new ChatRoom());
        mv.setViewName("user");
        mv.addObject("user", findUser);
        mv.addObject("roomList", roomList);
        return mv;
    }

    @GetMapping(value = "/count")
    public int countUser(@RequestBody User user) {
        return userService.countUser(user);
    }

    @GetMapping(value = "/{id}")
    public User getUser(@PathVariable("id") String id) {
        return userService.findUser(new User(id));
    }

    @GetMapping(value = "/list")
    public List<User> getUserList(@RequestBody User user) {
        return userService.findUserList(user);
    }

    @PostMapping(value = "/update")
    public int updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @PostMapping(value = "/inser")
    public int insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    @PostMapping(value = "/delete")
    public int deleteUserById(String id) {
        return userService.deleteUserById(id);
    }


}
